<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id"> {{ item.name }} </el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/settings'
import { getUserDatail } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存当前选中的权限列表
      rolesList: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 父传子 关闭弹层
    closeDialog() {
      this.$emit('close')
    },
    // 获取所有角色列表
    async loadRoles() {
      try {
        const { data: res } = await getRoles()
        console.log('getRoles获取分配', res)
        // 保存数据
        this.rolesList = res.rows

        const info = await getUserDatail(this.curId)
        console.log('getUserDatail', info)
        this.roleIds = info.data.roleIds
      } catch (error) {
        console.log(error)
      }
    },
    // 修改角色
    async hSubmit() {
      try {
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        console.log('assignRoles保存角色', res)
        // 关闭弹层刷新页面
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
